package com.lightjet.macross.base.awf.service.impl;

import com.lightjet.macross.base.awf.domain.BizProcessConfig;
import com.lightjet.macross.base.awf.repository.BizProcessConfigRepository;
import com.lightjet.macross.base.awf.service.BizProcessConfigService;
import com.lightjet.macross.common.core.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author : lijia
 * @version : 1.0 2019-06-18 11:23
 * description : 流程配置表服务层接口实现
 */
@Service
public class BizProcessConfigServiceImpl extends BaseServiceImpl<BizProcessConfig,Integer> implements BizProcessConfigService {

    /**
     * description:根据流程定义ID（或Key）、获取流程节点定义ID、配置类型获取节点配置信息
     *
     * @param pdid
     * @param nodeid
     * @param type
     */
    @Override
    public BizProcessConfig GetConfigValue(String pdid, String nodeid, BizProcessConfig.ConfigTypes type) {
        List<BizProcessConfig> list = ((BizProcessConfigRepository) baseRepository).findByProcinstdefIdAndNodedefIdAndConfigtype(pdid,nodeid,type);
        if((list==null||list.size()==0)&& (pdid.contains(":")))//如果按照流程含版本号的key找不到索引 则尝试 不含版本号的流程set key
        {
            list =((BizProcessConfigRepository) baseRepository).findByProcinstdefIdAndNodedefIdAndConfigtype(pdid.split(":")[0],nodeid,type);
        }
        if(list!=null&&list.size()>0)
        {
            return list.get(0);
        }
        else
        {
            return null;
        }
    }

    /**
     * Description //获取索流程定义分组集合
     *
     * @return java.util.List<java.lang.String>
     * CreateTime 2019/5/4 14:26
     * @author lijia
     * @Param []
     **/
    @Override
    public List<String> FindProcinstDefIdGroup() {
        return ((BizProcessConfigRepository) baseRepository).findProcinstDefIdGroup();
    }

    @Override
    public Map<String, String> FindNodeConfigMap(String pdid, BizProcessConfig.ConfigTypes type) {
        List<BizProcessConfig> list = ((BizProcessConfigRepository) baseRepository).findByProcinstdefIdAndConfigtype(pdid,type);
        if((list==null||list.size()==0)&& (pdid.contains(":")))//如果按照流程含版本号的key找不到索引 则尝试 不含版本号的流程set key
        {
            list = ((BizProcessConfigRepository) baseRepository).findByProcinstdefIdAndConfigtype(pdid.split(":")[0],type);
        }
        return list.stream().collect(Collectors.toMap(BizProcessConfig::getNodedefId, BizProcessConfig::getConfigvalue));

    }
}
